package com.stu.stusystem.service.system;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.stu.stusystem.common.ApiException;
import com.stu.stusystem.mapper.system.StuClassMapper;
import com.stu.stusystem.model.em.TreeModel;
import com.stu.stusystem.model.system.StuClass;
import com.stu.stusystem.model.system.StuUser;
import com.stu.stusystem.service.system.dto.StuClassDTO;
import com.stu.stusystem.service.system.dto.StuClassQuery;
import com.stu.stusystem.service.system.vo.StuClassVO;
import com.stu.stusystem.util.CopyBeanUtil;
import com.stu.stusystem.util.TreeModelUtil;
import com.stu.stusystem.util.UUIDUtil;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;
import tk.mybatis.mapper.weekend.WeekendSqls;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

/**
 * @author cxt
 * @date 2020/9/24
 */
@Service
public class StuClassService {

    private StuClassMapper stuClassMapper;

    /**
     * 分页查询列表
     */
    public PageInfo<StuClassVO> getStuClassPage(StuClassQuery query) {
        PageHelper.startPage(query.getPage(), query.getLimit());
        List<StuClassVO> stuClassPage = this.stuClassMapper.getStuClassPage(query);
        return new PageInfo<>(stuClassPage);
    }

    /**
     * 修改
     */
    public void updateStuClass(StuClassDTO dto) {
        StuClass stuClass = new StuClass();
        CopyBeanUtil.copyIgnore(dto, stuClass);
        StuUser user = (StuUser) SecurityUtils.getSubject().getPrincipal();
        stuClass.setUpdateBy(user.getName());
        stuClass.setUpdateTime(new Date());
        int i = this.stuClassMapper.updateByExampleSelective(stuClass, new Example.Builder(StuClass.class)
                .where(WeekendSqls.<StuClass>custom().andEqualTo(StuClass::getId, dto.getId())).build());
        if (i != 1) {
            throw new ApiException(ApiException.UPDATE_FAIL);
        }
    }

    /**
     * 删除班级
     */
    public void deleteStuClass(String id) {
        int i = this.stuClassMapper.deleteByPrimaryKey(id);
        if (i != 1) {
            throw new ApiException(ApiException.DELETE_FAIL);
        }
    }

    /**
     * 新增班级
     */
    public void saveStuClass(StuClassDTO dto) {
        List<StuClass> stuClasses = this.stuClassMapper.selectByExample(new Example.Builder(StuClass.class)
                .where(WeekendSqls.<StuClass>custom().andEqualTo(StuClass::getName, dto.getName())).build());
        if (stuClasses.size() != 0) {
            throw new ApiException("您添加的班级已存在");
        }
        StuClass stuClass = new StuClass();
        CopyBeanUtil.copyIgnore(dto, stuClass);
        StuUser user = (StuUser) SecurityUtils.getSubject().getPrincipal();
        stuClass.setCreateBy(user.getName());
        stuClass.setCreateTime(new Date());
        stuClass.setId(UUIDUtil.generate());
        int insert = this.stuClassMapper.insert(stuClass);
        if (insert != 1) {
            throw new ApiException(ApiException.SAVE_FAIL);
        }
    }

    /**
     * 查询全部的班级:树形结构
     */
    public List<Object> getStuClassTreeData() {
        List<TreeModel> models = this.stuClassMapper.treeSelect();
        return TreeModelUtil.buildTreeList(models, "pid");
    }

    /**
     * 查询全部的系和学院
     */
    public HashMap<String, ArrayList<StuClassVO>> getNonClass() {
        List<StuClassVO> nonClass = this.stuClassMapper.getNonClass();
        HashMap<String, ArrayList<StuClassVO>> hashMap = new HashMap<>();
        ArrayList<StuClassVO> department = new ArrayList<>();
        ArrayList<StuClassVO> college = new ArrayList<>();
        for (StuClassVO vo : nonClass) {
            if (vo.getSign().equals(1)) {
                college.add(vo);
            } else {
                department.add(vo);
            }
        }
        hashMap.put("department",department);
        hashMap.put("college",college);
        return hashMap;
    }

    @Autowired
    public void setStuClassMapper(StuClassMapper stuClassMapper) {
        this.stuClassMapper = stuClassMapper;
    }
}
